package org.ResearchProject.utils;

import org.ResearchProject.dto.SendMassageDTO;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author WangTing
 * @since 2024/12/14 16:30
 */
@Component
public class SendMassageUtil {

    @Resource
    private JavaMailSender javaMailSender;

    public void sendMsg(SendMassageDTO sendMassageDTO) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(sendMassageDTO.getFrom());
        helper.setSubject(sendMassageDTO.getSubject());
        helper.setText(sendMassageDTO.getContent(), true);
       // File file = new File(sendMassageDTO.getFilePath());
       // helper.addAttachment(file.getName(), file);
        helper.setTo(sendMassageDTO.getTo());
        javaMailSender.send(mimeMessage);
    }
}
